﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MMITWeb.Models;
using MMITWeb.Infrastructure.Abstract;

namespace MMITWeb.Areas.Teller.Controllers
{
    public class UserAccountController : Controller
    {
        IAuthProvider authProvider;

        public UserAccountController(IAuthProvider auth)
        {
            authProvider = auth;
        }
        
        public ActionResult Logon()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Logon(LogOnViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (authProvider.Authenticate(model.Username, model.Password))
                {
                    return Redirect(returnUrl ?? Url.Action("Index", "Home"));
                }
                else
                {
                    ModelState.AddModelError("", "Incorrect username or password");
                    return View();
                }
            }
            else
            {

                return View(model);
            }
        }

        public ActionResult LogOff()
        {
            authProvider.LogOff();
            return RedirectToAction("LogOn", "UserAccount");
        }

    }
}
